
<template>
  <div class="container">
    <div class="login">
      <el-form label-position="left" label-width="0px" class="login_form" :rules="rules" ref="ruleForm" :model="ruleForm">
        <h3 class="title">用户登录</h3>
        <el-form-item prop="username">
          <el-input type="text" placeholder="账号" v-model="ruleForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="密码" v-model="ruleForm.password" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" class="button" @click="tologin('ruleForm')" :loading="logining">登录</el-button>
    </div>
  </div>
</template>
<script>
import { login } from "../api/userMG";
import { setCookie, getCookie, delCookie } from "../utils/util";
export default {
  data() {
    //利用element-ui实现登录验证功能 
    //几个重点的属性分别是ruleForm rules 
    return {
      //定义login默认为false
      logining: false,
      //是否记住密码
      rememberpwd: false,
      //默认值
      ruleForm: {
        username: "",
        password: ""
      },
      //账号密码验证规则
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  created() {
    this.$message({
      message: "账号密码不为空即可",
      type: "success"
    });
  },
  methods: {
    tologin(formName) {
      console.log( this.$refs[formName])
      this.$refs[formName].validate(valid=>{
        if(valid){
          this.loginging=true
          setTimeout(() => {
            this.logining=false
            this.$store.commit('login', 'true')
            this.$router.push({ path: '/goods/Goods' })
          }, 1000);
        }else{
          this.$message.error('请输入账号密码')
          this.logining=false
          return false
        }
      })
    }
  }
};
</script>
<style scoped>
* {
  padding: 0;
  margin: 0;
}
.button {
  position: absolute;
  bottom: 20px;
  left: 20px;
  width: 90%;
}
.login_form {
  position: absolute;
  left: 20px;
  top: 20px;
  width: 90%;
  box-sizing: border-box;
  padding: 20px;
  text-align: center;
  line-height: 50px;
}

.container {
  height: 100%;
  background-color: #2b4b6b;
}
.login {
  border: 1px solid gray;
  height: 300px;
  width: 400px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  border-radius: 20px;
}
</style>>
  